var base_url="http://"+location.host+"/";
var cookie=document.cookie;
var cookie_list=cookie.split(";");
var img='',intro='';
cookie_list.forEach(function(v,i){
console.log(v.indexOf("img="))
    if(v.indexOf(" img=")==0){
        img=v.substr(v.indexOf("=")+1);
    }
    if(v.indexOf(" intro=")==0){
        intro=v.substr(v.indexOf("=")+1);
    }
});
$(".book_img").attr("src",img);
$(".book_intro").text(intro);

//章节目录点击事件
$("#catalog").click(function(e){
    req_url=base_url+"catalog";
    $.ajax({
        url:req_url,
        type:"POST",
        success:function(res){
            console.log(res);
            list=[];
            res.data.forEach(function(v,i){
                list.push({
                    title:v.name,
                    value:v.id
                });
            });
            $("#catalog").select({
                title:"章节目录",
                items:list
            });
        }
    });



});

//收藏点击事件
$(".collect").click(function(e){
    var is_collect=false;
    var book_id=$(this).attr("data_id");
    var cookie_list=document.cookie.split(";");
    var book_img="";
    var collects="";
    var collect_imgs="";
    var date = new Date();
    date.setTime(date.getTime()+7*24*60*60*1000);
    cookie_list.forEach(function(v,i){
        if(v.indexOf(" collects=")==0){
            collects=v.substr(v.indexOf("=")+1);
        }
        if(v.indexOf(" collect_imgs=")==0){
            collect_imgs=v.substr(v.indexOf("=")+1);
        }
        if(v.indexOf(" img=")==0){
            book_img=v.substr(v.indexOf("=")+1);
        }
    });

    if(collects==""&&collect_imgs==""){
        document.cookie="collects="+book_id+";path=/;expires="+date.toGMTString();
        document.cookie="collect_imgs="+book_img+";path=/;expires="+date.toGMTString();
    }else{
        collects.split(",").forEach(function(v,i){
            if(parseInt(v)==book_id){
                $.toast("已收藏过!", "forbidden");
                is_collect=true;
            }
        });
        if(is_collect){
            return;
        }
        document.cookie="collects="+collects+","+book_id+";path=/;expires="+date.toGMTString();
        document.cookie="collect_imgs="+collect_imgs+","+book_img+";path=/;expires="+date.toGMTString();
    }
    $.toast("收藏成功");
});